﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.ComponentModel;
using System.Reflection;

namespace DongFangGuoKai.TaskPlatform.WebApi.Extensions
{
    /// <summary>
    /// Swagger扩展
    /// </summary>
    public static class SwaggerExention
    {
        /// <summary>
        /// 注入自定义Swagger   
        /// </summary>
        /// <param name="builder"></param>
        public static void SwaggerExtend(this WebApplicationBuilder builder)
        {
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen(option =>
            {
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
            {
                option.SwaggerDoc(version, new OpenApiInfo()
                {
                    Title = $"Quartz.Framework接口文档",
                    Version = version,
                    Description = $"通用版本的CoreApi版本{version}"
                });
            });

                #region v2.
                //var versionArr = typeof(ApiVersions).GetFields();

                //foreach (var item in versionArr)
                //{
                //    DescriptionAttribute attribute = item.GetCustomAttribute<DescriptionAttribute>();
                //    string versionName = string.Empty;
                //    if (attribute != null)
                //    {
                //        versionName = attribute.Description;
                //        option.SwaggerDoc(versionName, new OpenApiInfo()
                //        {
                //            Title = $"东方国凯调度平台",
                //            Version = versionName,
                //            Description = $"通用版本的CoreApi版本{versionName}"
                //        });
                //    }
                #endregion

                //xml文档绝对路径
                var file = Path.Combine(AppContext.BaseDirectory, "DongFangGuoKai.TaskPlatform.WebApi.xml");
                //true : 显示控制器层注释
                option.IncludeXmlComments(file, true);
                //对action的名称进行排序。
                option.OrderActionsBy(o => o.RelativePath);
            });


        }

        /// <summary>
        /// Swagger中间件 
        /// </summary>
        /// <param name="app"></param>
        public static void UseSwaggerExtend(this WebApplication app)
        {
            if (app.Environment.IsDevelopment())
            {

            }
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                typeof(ApiVersions).GetEnumNames().ToList().ForEach((version) =>
                {
                    c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Quartz第【{version}】版本");
                });


                #region v2.  
                //var versionArr = typeof(ApiVersions).GetFields();

                //foreach (var item in versionArr)
                //{
                //    DescriptionAttribute attribute = item.GetCustomAttribute<DescriptionAttribute>();
                //    string versionName = string.Empty;
                //    if (attribute != null)
                //    {
                //        versionName = attribute.Description;
                //        c.SwaggerEndpoint($"/swagger/{versionName}/swagger.json", $"Quartz第【{versionName}】版本");
                //    }
                //}
                #endregion
            });
        }

    }
    public enum ApiVersions
    {
        [Description("海宁")]
        V1,
        [Description("蒙牛")]
        V2
    }

}
